<template>
    <page-container>
        <div class="ant-pro-table">
            <div class="ant-pro-table-search">
                <a-form layout="horizontal">
                    <a-row :gutter="16" type="flex" justify="start">
                        <a-col :xs="12" :sm="12" :md="6" :lg="3">
                            <a-form-item>
                                <a-select
                                    v-model:value="queryParams.type"
                                    :options="[
                                        {
                                            label: '安卓',
                                            value: 0,
                                        },
                                        {
                                            label: 'IOS',
                                            value: 1,
                                        },
                                    ]"
                                    allow-clear
                                    style="width: 100%"
                                    @change="handleSearch()"
                                ></a-select>
                            </a-form-item>
                        </a-col>
                        <a-col :xs="12" :sm="12" :md="7" :lg="5">
                            <a-form-item>
                                <a-range-picker v-model:value="queryParams.createTime" :placeholder="['开始时间', '结束时间']" allow-clear @change="handleSearch()" />
                            </a-form-item>
                        </a-col>
                        <a-col :xs="12" :sm="12" :md="5">
                            <a-form-item>
                                <a-input-search placeholder="输入版本号" v-model:value="queryParams.version" allow-clear enter-button @search="handleSearch()" />
                            </a-form-item>
                        </a-col>
                        <a-col :xs="12" :sm="12" :md="5">
                            <a-form-item>
                                <a-input-search placeholder="输入更新内容" v-model:value="queryParams.content" allow-clear enter-button @search="handleSearch()" />
                            </a-form-item>
                        </a-col>
                        <a-col :xs="24" :sm="24" :md="12" :lg="4">
                            <!-- <a-button @click="handleSearch" type="primary">搜索</a-button> -->
                            <a-button @click="handleReset" class="ml-5px">重置</a-button>
                        </a-col>
                    </a-row>
                </a-form>
            </div>
            <a-card :body-style="{ padding: 0 }" ref="elRef">
                <div class="ant-pro-table-list-toolbar">
                    <div class="ant-pro-table-list-toolbar-container">
                        <div class="ant-pro-table-list-toolbar-left"></div>
                        <div class="ant-pro-table-list-toolbar-right">
                            <a-space align="center">
                                <a-switch checked-children="显示" un-checked-children="隐藏" @change="handleSwitchChange" v-model:checked="checked" />
                                <a-button type="primary" @click="handleEdit({})">
                                    <plus-outlined />
                                    新增
                                </a-button>
                            </a-space>
                            <div class="ant-pro-table-list-toolbar-divider">
                                <a-divider type="vertical" />
                            </div>
                            <div class="ant-pro-table-list-toolbar-setting-item">
                                <a-tooltip title="刷新">
                                    <reload-outlined @click="reload" />
                                </a-tooltip>
                            </div>
                            <div class="ant-pro-table-list-toolbar-setting-item">
                                <a-tooltip title="密度">
                                    <a-dropdown :trigger="['click']" placement="bottomRight">
                                        <column-height-outlined />
                                        <template #overlay>
                                            <a-menu
                                                style="width: 80px"
                                                :selected-keys="[state.tableSize]"
                                                @click="
                                                    ({ key }) => {
                                                        fetchDataContext.tableSize = String(key);
                                                    }
                                                "
                                            >
                                                <a-menu-item key="default">
                                                    <a href="javascript:;">默认</a>
                                                </a-menu-item>
                                                <a-menu-item key="middle">
                                                    <a href="javascript:;">中等</a>
                                                </a-menu-item>
                                                <a-menu-item key="small">
                                                    <a href="javascript:;">紧凑</a>
                                                </a-menu-item>
                                            </a-menu>
                                        </template>
                                    </a-dropdown>
                                </a-tooltip>
                            </div>
                            <div class="ant-pro-table-list-toolbar-setting-item">
                                <a-popover placement="bottomRight" arrowPointAtCenter trigger="click" overlayClassName="ant-pro-table-column-setting-overlay">
                                    <template #title>
                                        <div class="ant-pro-table-column-setting-title">
                                            <a-checkbox v-model:checked="columnState.checkAll" :indeterminate="columnState.indeterminate" @change="handleColumnAllClick">
                                                列展示 / 排序
                                            </a-checkbox>
                                            <a @click="reset">重置</a>
                                        </div>
                                    </template>
                                    <template #content>
                                        <span class="ant-pro-table-column-setting-list">
                                            <drag-container
                                                lockAxis="y"
                                                dragClass="ant-pro-table-drag-ghost"
                                                dropClass="ant-pro-table-drop-ghost"
                                                @drop="({ removedIndex, addedIndex }) => move(removedIndex, addedIndex)"
                                            >
                                                <template :key="column.key" v-for="column in dynamicColumnItems">
                                                    <draggable>
                                                        <div class="ant-pro-table-column-setting-list-item">
                                                            <drag-icon />
                                                            <a-checkbox :checked="column.checked" @change="handleColumnChange($event, column)">
                                                                {{ column.label }}
                                                            </a-checkbox>
                                                        </div>
                                                    </draggable>
                                                </template>
                                            </drag-container>
                                        </span>
                                    </template>
                                    <a-tooltip title="列设置">
                                        <setting-outlined />
                                    </a-tooltip>
                                </a-popover>
                            </div>
                            <!-- <div class="ant-pro-table-list-toolbar-setting-item">
                                <a-tooltip :title="screenState ? '退出全屏' : '全屏'">
                                    <fullscreen-outlined v-if="!screenState" @click="setFull" />
                                    <fullscreen-exit-outlined v-else @click="exitFull" />
                                </a-tooltip>
                            </div> -->
                        </div>
                    </div>
                </div>
                <a-table
                    row-key="id"
                    :size="state.tableSize"
                    :loading="state.loading"
                    :columns="dynamicColumns"
                    :data-source="state.dataSource"
                    :pagination="{
                        'show-size-changer': true,
                        current: state.current,
                        pageSize: state.pageSize,
                        total: state.total,
                        showTotal: total => `共 ${total} 条`,
                    }"
                    @change="handleTableChange"
                >
                    <template #bodyCell="{ record, column }">
                        <template v-if="column.dataIndex === 'type'">
                            {{ record.type == 1 ? 'IOS' : '安卓' }}
                        </template>
                        <template v-if="column.dataIndex === 'action'">
                            <a-space>
                                <!-- <a @click="handleEdit(record)">复制地址</a> -->
                                <a @click="handleEdit(record)">编辑</a>
                                <a-popconfirm title="确定要删除吗 ?" @confirm="onDelete(record)">
                                    <a-button type="text" danger size="small">删除</a-button>
                                </a-popconfirm>
                            </a-space>
                        </template>
                    </template>
                </a-table>
            </a-card>
        </div>
        <app-modal :model="roleModalRef" :visible="modalVisible" @cancel="modalVisible = false" @success="reload" />
    </page-container>
</template>

<script lang="ts">
    import { defineComponent, reactive, ref, onMounted } from 'vue';
    import { PlusOutlined, ReloadOutlined, ColumnHeightOutlined, SettingOutlined, FullscreenOutlined, FullscreenExitOutlined } from '@ant-design/icons-vue';
    import { Container as DragContainer, Draggable } from '@/components/draggable';
    import { deleteApp, getAppPage, getSmslogin, smsloginStatus } from '@/api/common/app';
    import type { Pagination, TableColumn } from '@/typing';
    import { useFetchData } from '@/utils/hooks/useFetchData';
    import { useFullscreen } from '@/utils/hooks/useFullscreen';
    import { useTableDynamicColumns } from '@/utils/hooks/useTableColumn';
    import DragIcon from '@/components/table/drag-icon.vue';
    import AppModal from './app-modal.vue';
    import { message } from 'ant-design-vue';
    import { isArray } from 'lodash-es';

    const baseColumns: TableColumn[] = [
        {
            title: '平台',
            dataIndex: 'type',
        },
        {
            title: '版本号',
            dataIndex: 'version',
        },
        {
            title: '发布时间',
            dataIndex: 'createTime',
        },
        // {
        //     title: '状态',
        //     dataIndex: 'status',
        // },
        {
            title: '发布描述',
            dataIndex: 'content',
            ellipsis: true,
        },
        {
            title: '操作',
            dataIndex: 'action',
        },
    ];
    export default defineComponent({
        name: 'APPList',
        setup() {
            const roleModalRef = ref<any>({});
            const {
                state: columnState,
                dynamicColumns,
                dynamicColumnItems,
                handleColumnAllClick,
                handleColumnChange,
                reset,
                move,
            } = useTableDynamicColumns(baseColumns, { needRowIndex: false });
            const [elRef, screenState, { setFull, exitFull }] = useFullscreen();
            const queryParams = reactive<any>({
                type: 0,
            });
            const fetchDataContext = reactive<any>({
                current: 1,
                pageSize: 10,
                tableSize: 'middle', // 'default' | 'middle' | 'small'
                stripe: true,
                requestParams: {
                    ...queryParams,
                },
            });
            const statusMap = {
                0: { text: '停用', status: 'error' },
                1: { text: '正常', status: 'processing' },
            };
            // 表格数据和加载
            const { reload, context: state } = useFetchData(getAppPage, fetchDataContext);
            const handleTableChange = ({ current, pageSize }: Pagination) => {
                fetchDataContext.current = current;
                fetchDataContext.pageSize = pageSize;
            };
            // 新增修改表单
            const modalVisible = ref<boolean>(false);
            const handleEdit = (record: Record<string, any>) => {
                roleModalRef.value = record;
                modalVisible.value = true;
            };

            const onDelete = (data: any) => {
                deleteApp(data.id).then(res => {
                    if (res.data) {
                        message.success('删除成功');
                        reload();
                    }
                });
            };

            const handleSearch = () => {
                const { createTime = [], ...lastParams } = queryParams;
                if (isArray(createTime) && createTime.length === 2) {
                    lastParams.createTimeStart = createTime[0];
                    lastParams.createTimeEnd = createTime[1];
                } else {
                    delete lastParams.createTimeStart;
                    delete lastParams.createTimeEnd;
                }
                fetchDataContext.current = 1;
                fetchDataContext.requestParams = { ...lastParams };
            };
            const handleReset = () => {
                queryParams.type = 0;
                fetchDataContext.current = 1;
                fetchDataContext.requestParams = { ...queryParams };
            };
            const checked = ref<boolean>(false);

            onMounted(() => {
                getSmslogin().then((res: any) => {
                    checked.value = res.data.status == 1;
                });
            });

            const handleSwitchChange = e => {
                smsloginStatus(e ? 1 : 0).then(() => {
                    getSmslogin().then((res: any) => {
                        checked.value = res.data.status == 1;
                    });
                });
            };

            return {
                handleSwitchChange,
                checked,
                queryParams,
                reload,
                state,
                columnState,
                dynamicColumns,
                dynamicColumnItems,
                statusMap,
                // fullscreen
                elRef,
                screenState,
                setFull,
                exitFull,
                handleEdit,
                handleTableChange,

                handleColumnChange,
                handleColumnAllClick,
                reset,
                move,

                onDelete,

                // 表单
                modalVisible,

                roleModalRef,
                fetchDataContext,

                handleSearch,

                handleReset,
            };
        },
        components: {
            DragIcon,
            PlusOutlined,
            ReloadOutlined,
            ColumnHeightOutlined,
            SettingOutlined,
            FullscreenOutlined,
            FullscreenExitOutlined,
            Draggable,
            DragContainer,
            AppModal,
        },
    });
</script>
